import KoaRouter from 'koa-router'
import ljwt from '../lib/ljwt'
import Lresponse from '../modules/lresponse'

const router = new KoaRouter()

router.get('/', async (ctx, next) => {
  const userInfo = ctx.request['userInfo']
  const res = new Lresponse()
  res.result = userInfo
  ctx.body = res
})

router.post('/login', async (ctx, next) => {
  console.log(ctx.request.body)
  // const userInfo = JSON.parse(ctx.request.body.toString()) // 这里验证登录，取用户ID等关键信息
  const userInfo = ctx.request.body // 这里验证登录，取用户ID等关键信息
  const token = ljwt.sign(userInfo)
  const res = new Lresponse()
  res.result = {
    token
  }
  ctx.body = res.toJson()
})

export default router
